<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class template_type_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template_type.template_type_list_model');
		$col = $CI->uri->segment($CI->template_type_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->template_type_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->template_type_list_model->cache->add($k, $order[$CI->template_type_list_model->cache->load($k)]);
			} else
				$CI->template_type_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->template_type_list_model->url_prefix.'/'.$CI->template_type_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template_type.template_type_list_model');
		
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				$CI->db->delete(TEMPLATE_TYPES, array(TEMPLATE_TYPES_TEMPLATE_TYPE_ID=>$id/*, TEMPLATE_TYPES_FRANCHISE_ID=>$CI->template_type_list_model->admin_id()*/));			
			}

			// message
			$CI->template_type_list_model->cache->add($CI->template_type_list_model->msg_cache_prefix, $CI->lang->line('list_delete_all'));
		} else
			// message
			$CI->template_type_list_model->cache->add($CI->template_type_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete'));
		
		// go to original page
		redirect($CI->template_type_list_model->url_prefix.'/'.$CI->template_type_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template_type.template_type_list_model');
		
		$id = $CI->uri->segment($CI->template_type_list_model->item_segment);
		$CI->db->delete(TEMPLATE_TYPES, array(TEMPLATE_TYPES_TEMPLATE_TYPE_ID=>$id/*, TEMPLATE_TYPES_FRANCHISE_ID=>$CI->template_type_list_model->admin_id()*/));
		
		// message
		$CI->template_type_list_model->cache->add($CI->template_type_list_model->msg_cache_prefix, $CI->lang->line('list_delete'));
		
		// go to original page
		redirect($CI->template_type_list_model->url_prefix.'/'.$CI->template_type_list_model->start);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template_type.template_type_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->template_type_detail_model->cache->add($CI->template_type_list_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());

			// go to original page
			redirect($CI->template_type_detail_model->url_prefix.'/'.$CI->template_type_detail_model->start.'/'.$CI->template_type_detail_model->item);
		}
		
		// save to template_type
		if ($CI->template_type_detail_model->hasTemplate_type())
		{
			// update
			$sql = "UPDATE ".TEMPLATE_TYPES." SET";
			$sql .= " ".TEMPLATE_TYPES_NAME."=".$CI->db->escape($CI->input->post('name'));
			$sql .= ", ".TEMPLATE_TYPES_NOTE."=".$CI->db->escape($CI->input->post('note'));
			//$sql .= " WHERE ".TEMPLATE_TYPES_FRANCHISE_ID."=".$CI->template_type_detail_model->admin_id();
			$sql .= " WHERE ".TEMPLATE_TYPES_TEMPLATE_TYPE_ID."=".$CI->template_type_detail_model->item;
			$CI->db->query($sql);
		} else {
			// insert
			$data = array(
				TEMPLATE_TYPES_NAME=>$CI->input->post('name'),
				TEMPLATE_TYPES_NOTE=>$CI->input->post('note'),
				//TEMPLATE_TYPES_FRANCHISE_ID=>$CI->template_type_detail_model->admin_id(),
				TEMPLATE_TYPES_TEMPLATE_TYPE_ID=>$CI->template_type_detail_model->item,
			);
			$CI->db->insert(TEMPLATE_TYPES, $data);
		}

		// message
		$CI->template_type_detail_model->cache->add($CI->template_type_detail_model->msg_cache_prefix, $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->template_type_detail_model->url_prefix2.'/'.$CI->template_type_detail_model->start);
	}		
}

?>